package com.kingdee.pda.util;


import android.text.Html;
import android.text.TextUtils;

import java.text.DecimalFormat;

/**
 * String工具类
 *
 * @author ZhuHuaHua
 * @date 2018/12/25 15:51
 */
public class StringUtils {

    private StringUtils() {
        throw new AssertionError();
    }

    private static final String WXPAY_CODE_REGIX = "^(10|11|12|13|14|15)\\d{16}$";
    private static final String ALIPAY_CODE_REGIX = "^(25|26|27|28|29|30)\\d{14,22}$";

    /**
     * 去前后空格后判断字符串是否有值<br/>
     *
     * @param str
     * @return 如果为null或者长度为0或者只有空格返回true，其他情况返回false.
     */
    public static boolean isBlank(String str) {
        return (str == null || str.trim().length() == 0);
    }

    /**
     * 判断字符串是否为空
     *
     * @param str 传入的字符串
     * @return 如果为null或者长度为0返回true，其他情况返回false.
     */
    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }

    /**
     * 获取CharSequence的长度
     * length(null) = 0;
     * length(\"\") = 0;
     * length(\"abc\") = 3;
     *
     * @param str 传入的字符串
     * @return 如果为null或者空串return 0, 其他情况return {@link CharSequence#length()}.
     */
    public static int length(CharSequence str) {
        return str == null ? 0 : str.length();
    }

    /**
     * Returns true if a and b are equal, including if they are both null.
     * <p><i>Note: In platform versions 1.1 and earlier, this method only worked well if
     * both the arguments were instances of String.</i></p>
     *
     * @param a first CharSequence to check
     * @param b second CharSequence to check
     * @return true if a and b are equal
     */
    public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    public static CharSequence getHtmlStr(String content) {
        CharSequence charSequence;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            charSequence = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY);
        } else {
            charSequence = Html.fromHtml(content);
        }
        return charSequence;
    }

    /**
     * @param date
     * @return YYYY_MM_DD_HH_MM_SS
     */
    public static String getShowDateText(String date) {
        if (TextUtils.isEmpty(date) || date.length() <= DateFormatUtils.YYYY_MM_DD_HH_MM_SS.length()) {
            return date;
        }
        return date.substring(0, DateFormatUtils.YYYY_MM_DD_HH_MM_SS.length());
    }

    /**
     * @param date
     * @return YYYY_MM_DD
     */
    public static String getShowDateStr(String date) {
        if (!TextUtils.isEmpty(date) && date.length() > DateFormatUtils.YYYY_MM_DD.length()) {
            return date.substring(0, DateFormatUtils.YYYY_MM_DD.length());
        }
        return date;
    }

    public static String getShowDoubleStr(double d) {
        if (d == 0) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(d);
    }

    /**
     * 半角字符转全角字符
     * 解决TextView文本显示格式紊乱的问题
     *
     * @param input
     * @return
     */
    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    /**
     * 获取加密会员名称字符串，第二个字符加密
     *
     * @param vipName
     * @return
     */
    public static String getEncryptVipName(String vipName) {
        if (vipName.length() >= 2) {
            StringBuilder sb = new StringBuilder(vipName);
            return sb.replace(1, 2, "*").toString();
        }
        return vipName;
    }

    public static String getEncryptVipMobile(String mobile) {
        if (mobile.length() == 11) {
            StringBuffer sb = new StringBuffer(mobile);
            return sb.replace(3, 7, "****").toString();
        }
        return mobile;
    }

    private static String getEncryptVipCardNo(String vipCardNo) {
        if (!TextUtils.isEmpty(vipCardNo)) {
            StringBuffer sb = new StringBuffer(vipCardNo);
            if (vipCardNo.length() > 5) {
                return sb.replace(2, vipCardNo.length() - 2, getEncryptStr(vipCardNo.length() - 4)).toString();
            } else if (vipCardNo.length() > 3) {
                return sb.replace(2, vipCardNo.length() - 1, getEncryptStr(vipCardNo.length() - 3)).toString();
            } else {
                return vipCardNo;
            }
        }
        return "";
    }

    private static String getEncryptStr(int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append("*");
        }
        return sb.toString();
    }

    public static String getEncryptVipInfo(String mobile, String vipCardNo) {
        if (!TextUtils.isEmpty(mobile) && mobile.length() == 11) {
            return getEncryptVipMobile(mobile);
        }
        return getEncryptVipCardNo(vipCardNo);
    }

    /**
     * 是否为支付宝付款码
     *
     * @param payCode
     * @return
     */
    public static boolean isAlipayCode(String payCode) {
        return payCode.matches(ALIPAY_CODE_REGIX);
    }

    /**
     * 是否为微信付款码
     *
     * @param payCode
     * @return
     */
    public static boolean isWxpayCode(String payCode) {
        return payCode.matches(WXPAY_CODE_REGIX);
    }


    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
